package com.lxj.xianyu;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
public class JobProcessor implements PageProcessor {

    private String keyword;

    public JobProcessor(String keyword) {
        this.keyword = keyword;
    }

    @Override
    public void process(Page page) {
        List<Selectable> selectables = page.getHtml().css("div#J_ItemListsContainer div.item-block").nodes();

        List<Item> items = new ArrayList<>();
        for (Selectable selectable : selectables) {
            String div = selectable.get();
            items.add(saveInfo(Jsoup.parse(div)));
        }
        Collections.reverse(items);
        page.putField("items", items);
    }

    private Item saveInfo(Document document) {
        Item item = new Item();
        item.setKeyword(keyword);

        Elements nick = document.select(".seller-nick");
        Elements a = nick.select("a");

        log.info("卖家昵称：" + a.text());
        item.setSellerName(a.text());
        log.info("卖家链接：" + a.attr("href"));
        item.setSellerUrl(a.attr("href"));

        Elements pic = document.select(".item-pic");
        Elements a1 = pic.select("a");
        log.info("商品链接：" + a1.attr("href"));
        item.setUrl(a1.attr("href"));
        Elements img = pic.select("img");
        log.info("商品图片：" + img.attr("data-ks-lazyload-custom"));
        item.setPic(img.attr("data-ks-lazyload-custom"));
        log.info("商品标题：" + img.attr("title"));
        item.setTitle(img.attr("title"));

        Elements price = document.select(".price");

        log.info("商品价格：" + price.select("em").text());
        item.setPrice(price.select("em").text());

        Elements location = document.select(".item-location");
        log.info("商品位置：" + location.text());
        item.setLocation(location.text());

        Elements desc = document.select(".item-brief-desc");
        log.info("商品详情：" + desc.text());
        item.setDesc(desc.text());

        log.info("======================================");

        return item;
    }

    private Site site = Site
            .me()
            .setCharset("gbk")
            .setTimeOut(10000)
            .setRetryTimes(3)
            .setSleepTime(3);

    @Override
    public Site getSite() {
        return site;
    }

}
